using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml.Serialization;

namespace VodafoneWP7
{
    public class OurApplication
    {
        public string id;
        public int priority;
        public string name;
        public string description;    
        public string imageUri;
        public int revision;
    }

    public class SavedState
    {
        //public List<Score> scores = new List<Score>(); 
        //public string last_nick;
        //public GameDifficulty last_difficulty;
        //[XmlIgnore]
        public List<OurApplication> applications;
    }

    public static class ApplicationState
    {
        private static SavedState __state;
        [XmlIgnore]
        public static SavedState state
        {
            get
            {
                if (__state == null)
                    LoadSavedStateAndOptions();
                return __state;
            }
        }
                
        static public void LoadSavedStateAndOptions()
        {
            //RestoreOptions();
            RestoreState();

            if (state.applications == null)
            {
                state.applications = new List<OurApplication>();                
            }
        }


        static public void SaveStateAndOptions()
        {
            SaveState();
        }

        public static void RestoreState()
        {
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream stream = null;
            try
            {
                stream = store.OpenFile("state", FileMode.OpenOrCreate);

                XmlSerializer serializer = new XmlSerializer(typeof(SavedState));
                __state = serializer.Deserialize(stream) as SavedState;
                stream.Close();
            }
            catch (Exception ex)
            {
                if (stream != null)
                    stream.Close();
                store.Dispose();
                __state = new SavedState();
            }
            store.Dispose();
        }

        public static void SaveState()
        {
            try
            {
                IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream stream = store.OpenFile("state3", FileMode.Create, FileAccess.ReadWrite);

                XmlSerializer serializer = new XmlSerializer(typeof(SavedState));
                serializer.Serialize(stream, state);

                stream.Close();
            }
            catch (Exception e)
            {
                e = e;
            }

        }
   
    }
}
